package com.focus.wecenter.service.impl;

import com.focus.wecenter.dao.UserDao;
import com.focus.wecenter.dao.AnswerCommentDao;
import com.focus.wecenter.dao.AnswerDao;
import com.focus.wecenter.dao.QuestionDao;
import com.focus.wecenter.pojo.*;
import com.focus.wecenter.service.AnswerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Set;

/**
 * 回答模块业务实现
 *
 * @author Lv Jing
 * @date 2018年01月07日 15:41
 */
@Service
public class AnswerServiceImpl implements AnswerService {

    @Autowired
    private QuestionDao questionDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AnswerDao answerDao;
    @Autowired
    private AnswerCommentDao answerCommentDao;


    @Override
    public Answer saveAnswer(int questionID, String content, int userID) {
        // 查出问题，问题回答字段更新
        Question question = questionDao.findById(questionID);
        question.setAnswerCount(question.getAnswerCount() + 1);
        question.getUser().setIntegral(question.getUser().getIntegral() + 10);
        Set<Topic> involvedTopics = question.getInvolvedTopics();
        for (Topic topic : involvedTopics) {
            topic.setDiscussCount(topic.getDiscussCount() + 1);
        }
        // 查出用户，用户回答字段更新
        User user = userDao.findById(userID);
        user.setIntegral(user.getIntegral() + 1);
        user.setAnswerCount(user.getAnswerCount() + 1);
        // 保存回答
        Answer answer = new Answer();
        answer.setAnswerContent(content);
        answer.setUser(user);
        answer.setQuestion(question);
        answerDao.add(answer);
        return answer;
    }

    @Override
    public void voteAnswer(int answerID, int type) {
        Answer answer = answerDao.findById(answerID);
        if (type == 1) {
            answer.setAgreeCount(answer.getAgreeCount() + 1);
            answer.getUser().setIntegral(answer.getUser().getIntegral() + 1);
            answer.getUser().setAgreeCount(answer.getUser().getAgreeCount() + 1);
            return;
        }
        if (type == -1) {
            answer.setAgainstCount(answer.getAgainstCount() + 1);
            answer.getUser().setIntegral(answer.getUser().getIntegral() - 1);
        }
    }

    @Override
    public Set<AnswerComment> findAnswerCommentsByID(int answerID) {
        Answer answer = answerDao.findById(answerID);
        return answer == null?null:answer.getComments();
    }

    @Override
    public AnswerComment saveAnswerComment(String message, int userID, int answerID) {
        // 查出回答，评论数更新
        Answer answer = answerDao.findById(answerID);
        answer.setCommentCount(answer.getCommentCount() + 1);
        answer.getQuestion().setCommentNumber(answer.getQuestion().getCommentNumber() + 1);
        answer.getUser().setIntegral(answer.getUser().getIntegral() + 2);
        // 评论保存
        AnswerComment comment = new AnswerComment();
        User user = userDao.findById(userID);
        user.setIntegral(user.getIntegral() + 1);
        comment.setUser(user);
        comment.setContent(message);
        answer.getComments().add(comment);
        answerCommentDao.add(comment);
        return comment;
    }
}
